//Requires Webpush.js (Sockets) & jquery.js (Ajax)

var Action = {}
Action.url = "actions.php";
Action.socket = null;

//AJAX REQUESTS
Action.GetTable = function() 
{
	$.ajax(
	{
		type : "POST",
		url : Action.url,
		dataType: "json",
		async : false,
		data: "action=gettable",
		success : function()
		{
			//TODO:
		}
	});
}

//SOCKETS REQUESTS
Action.OpenSocket = function()
{
	Action.socket = new WebPush('ws://localhost:8081');
	
	//Eventos
	Action.socket.bind('open', function()
    {
    	console.log("Socket: Open!!");
    	
    	$("#socket_status").html("Connected");
    	$("#socket_status").removeClass();
    	$("#socket_status").addClass("green");
    });

    Action.socket.bind('connection_disconnected', function() 
    {
        console.log("Socket: Disconected!!");
    });

    Action.socket.bind('close', function() 
    {
        console.log("Socket: Close!!");
        
        $("#socket_status").html("Disconnected");
    	$("#socket_status").removeClass();
    	$("#socket_status").addClass("red");
    });

    Action.socket.bind('connection_failed', function() 
    {
        console.log("Socket: Failed!!");
    });

    Action.socket.bind('message', function(data) 
    {
        console.log("Socket: Message!! "+data);
        
        var response = JSON.parse(data);
        //alert("ServerCallbacks."+response['action']+'(response)');
        eval("ServerCallbacks."+response['action']+'(response)');
    });
}

Action.UpdatePlayerPosition = function(player)
{
	var data = '{"action":"UpdatePlayerPosition","data":{"id":"'+player.id+'","X":'+player.posX+',"Y":'+player.posY+'}}';
	
	console.log(data);
	
	Action.socket.send(data);
}